#include "filerw.h"
#include <sys/stat.h>

FileReader::FileReader(const char *fileName)
{
	fin = open(fileName, O_RDONLY);
	//cout << fin;    
}

FileReader::~FileReader()
{
	close(fin);
}

bool FileReader::asyncRead(char *buffer, int offset, int len, Callback callback)
{
	if (fin == -1)
	{
		return false;
	}  
	else 
	{
		int end;
		lseek(fin, offset, SEEK_CUR);
		end = read(fin, buffer, len);
		buffer[end] = '\0';
		//cout << buffer;
		return true;
	}
}

FileWriter::FileWriter(const char *fileName)
{
	fout = open(fileName, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
}

FileWriter::~FileWriter()
{
	close(fout);
}

bool FileWriter::asyncWrite(const char *buffer, int offset, int len, Callback callback)
{
	if (fout == -1)
	{
		return false;
	} 
	else
	{
		lseek(fout, offset, SEEK_CUR);
		write(fout, buffer, len);
		return true;
	}
}

